package com.jn.erp.wechat.mp.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.jn.erp.wechat.mp.entity.ReceiveMessage;
//import com.sun.corba.se.impl.protocol.giopmsgheaders.ReplyMessage;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

import java.io.StringWriter;

/**
 * <pre>
 *
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/11/7 8:58
 */
public class XMLUtil {
    public static void main(String[] args) {
        String str = "<xml>\n" +
                "  <ToUserName><![CDATA[toUser]]></ToUserName>\n" +
                "  <FromUserName><![CDATA[FromUser]]></FromUserName>\n" +
                "  <CreateTime>123456789</CreateTime>\n" +
                "  <MsgType><![CDATA[event]]></MsgType>\n" +
                "  <Event><![CDATA[subscribe]]></Event>\n" +
                "</xml>";
        ReceiveMessage rm=XMLUtil.XMLTOModel(str);
        System.out.println(rm);
        rm.setCreateTime(DateUtil.current()+"");
        rm.setContent("中文测试一条数据");
        String sss=XMLUtil.buildResponseMessage(rm);
        System.out.println(sss);
        String key="qrscene_notice_oYL9_7Uk0BMib0QCaPH4ODFiAOgQ";
        boolean notice = StrUtil.startWith(key, "qrscene_notice_");
        System.out.println(notice);
        String sub = StrUtil.subSuf(key, 15);
        System.out.println(sub);
        String content="员工15840142750";
        String mobile=StrUtil.subSuf(content, 2);
        System.out.println(mobile);

    }
    public  static ReceiveMessage XMLTOModel(String str) {
        ReceiveMessage receiveMessage = new ReceiveMessage();
        try {
            Document document = DocumentHelper.parseText(String.valueOf(str));
            Element root = document.getRootElement();
            receiveMessage.setToUserName(root.elementText("ToUserName"));
            receiveMessage.setFromUserName(root.elementText("FromUserName"));
            receiveMessage.setMsgType(root.elementText("MsgType"));
            receiveMessage.setContent(root.elementText("Content"));
            receiveMessage.setCreateTime(root.elementText("CreateTime"));
            receiveMessage.setMsgId(root.elementText("MsgId"));
            receiveMessage.setMsgDataId(root.elementText("MsgDataId"));
            receiveMessage.setIdx(root.elementText("Idx"));
            receiveMessage.setEvent(root.elementText("Event"));
            receiveMessage.setEventKey(root.elementText("EventKey"));
            receiveMessage.setTicket(root.elementText("Ticket"));
        } catch (Exception e) {
            System.out.println(e);
        }
        return receiveMessage;
    }

//    public static String ObjToXml(ReplyMessage obj) throws Exception {
//        Document document = DocumentHelper.createDocument();
//        Element root = document.addElement(obj.getClass().getSimpleName());
//        convertObjectToXml(obj, root);
//        StringWriter stringWriter = new StringWriter();
//        XMLWriter writer = new XMLWriter(stringWriter);
//        writer.write(document);
//        writer.close();
//        return stringWriter.toString();
//    }
    private static void convertObjectToXml(Object obj, Element element) throws Exception {
        Class<?> clazz = obj.getClass();
        for (java.lang.reflect.Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            Element child = element.addElement(field.getName());
            Object value = field.get(obj);
            if (value != null) {
                if (value.getClass().isPrimitive() || value.getClass() == java.lang.String.class) {
                    child.setText(value.toString());
                } else {
                    convertObjectToXml(value, child);
                }
            }
        }
    }






    /**
     * 根据消息类型构造返回消息
     * @param map 封装了解析结果的Map
     * @return responseMessage(响应消息)
     */
    public static String buildResponseMessage(ReceiveMessage receiveMessage) {
        StringBuffer str = new StringBuffer();
        str.append("<xml>");
        str.append("<ToUserName><![CDATA[" +receiveMessage.getToUserName() + "]]></ToUserName>");
        str.append("<FromUserName><![CDATA[" + receiveMessage.getFromUserName() + "]]></FromUserName>");
        str.append("<CreateTime>" + receiveMessage.getCreateTime() + "</CreateTime>");
        str.append("<MsgType><![CDATA[" + receiveMessage.getMsgType()+ "]]></MsgType>");
        str.append("<Content><![CDATA[" + receiveMessage.getContent() + "]]></Content>");
        str.append("</xml>");
        return str.toString();
    }
}

